﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XingFuStudio.Pioneer.Net;
using XingFuStudio.Pioneer.Entity;
using XingFuStudio.Pioneer.IO;
using XingFuStudio.Pioneer.Container.Interface.Event;

namespace XingFuStudio.Pioneer.Applications.Garden {
    /// <summary>
    /// 农场管理类
    /// </summary>
    public class GardenManager {

        #region 事件

        /// <summary>
        /// 打印日志事件
        /// </summary>
        public event EventHandler<LogEventArgs> PrintLog = null;
        /// <summary>
        /// 出错事件
        /// </summary>
        public event EventHandler<XingFuStudio.Pioneer.Container.Interface.Event.ErrorEventArgs> Error = null;

        /// <summary>
        /// 执行打印日志事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="isNewLine"></param>
        /// <param name="formate"></param>
        /// <param name="args"></param>
        protected void DoPrintLog(bool isNewLine, string format, params object[] args) {
            if (this.PrintLog != null) {
                this.PrintLog(this, new LogEventArgs() {
                    IsNewLing = isNewLine, LogMessage = String.Format(format, args)
                });
            }
        }

        /// <summary>
        /// 执行出错事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="ex"></param>
        protected void DoError(Exception ex) {
            if (this.Error != null) {
                this.Error(this, new XingFuStudio.Pioneer.Container.Interface.Event.ErrorEventArgs() {
                    Exception = ex
                });
            }
        }

        #endregion

        /// <summary>
        /// 应用对应开心网编码
        /// </summary>
        private readonly int AppCode = 1062;

        /// <summary>
        /// Http请求管理类对象
        /// </summary>
        private HttpManager httpManager = null;

        /// <summary>
        /// 帐户实体对象
        /// </summary>
        private AccountInfo account = null;
        
        /// <summary>
        /// 帐户验证编码
        /// </summary>
        private string verifyCode = null;

        /// <summary>
        /// 获取或设置帐户
        /// </summary>
        public AccountInfo Account {
            get {
                return account;
            }
            set {
                account = value;
            }
        }

        /// <summary>
        /// 获取或设置Http请求管理类对象
        /// </summary>
        public HttpManager HttpManager {
            get {
                return httpManager;
            }
            set {
                httpManager = value;
            }
        }

        /// <summary>
        /// 创建一个<see cref="GardenManager"/>实例
        /// </summary>
        public GardenManager() {

        }

        /// <summary>
        /// 创建一个<see cref="GardenManager"/>实例
        /// </summary>
        /// <param name="httpManager"></param>
        /// <param name="account"></param>
        public GardenManager(HttpManager httpManager, AccountInfo account) {
            //初始化开心网Http请求处理类对象
            this.httpManager = httpManager;
            //初始化登录帐户
            this.account = account;
        }

        /// <summary>
        /// 运行管理
        /// </summary>
        public void Run() {
            //打印日志
            this.DoPrintLog(true, "开始自动管理花园模块相关功能。");
            //获取帐户验证码
            this.verifyCode = this.GetVerifyCode();

            this.DoPrintLog(true, verifyCode);
        }

        /// <summary>
        /// 获取帐户验证编码
        /// </summary>
        /// <returns></returns>
        private string GetVerifyCode() {
            //先访问花园主页
            string content = this.httpManager.Get("http://www.kaixin001.com/app/app.php?aid=1062&url=garden/index.php");

            //获取当前用户验证码
            string verifyCode = content.GetMidString("g_verify = \"", "\"");
            //返回当前用户验证码
            return verifyCode;
        }
    }
}
